<?php

namespace addons\qingdong\controller;

use addons\qingdong\model\Notice as NoticeModel;

/**
 * 公告接口
 */
class Notice extends StaffApi {
	protected $noNeedLogin = [];
	protected $noNeedRight = [];


	//公告列表
	public function getList() {
		$limit = input("limit/d", 10);


		$records = NoticeModel::where([])->order('id desc')->paginate($limit);
		$items   = $records->items();
		foreach ($items as $k => $v) {
			$read_staff_ids = explode(',', $v['read_staff_ids']);
			$v['is_show']   = in_array($this->auth->id, $read_staff_ids);
			$items[$k]      = $v;
		}
		$this->success('请求成功', $records);
	}


	//获取公告详情
	public function getDetail() {
		$id     = input('id', '', 'intval');
		$notice = NoticeModel::where(['id' => $id,])->find();
		if (empty($notice)) {
			$this->error('公告不存在');
		}
        preg_match_all('/<img.+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>/i',$notice['content'],$match);
        foreach ($match[1] as $v){
            $notice['content']=str_replace($v,cdnurl($v,true),$notice['content']);
        }
		$read_staff_ids = explode(',', $notice['read_staff_ids']);
		if (!in_array($this->auth->id, $read_staff_ids)) {
			$read_staff_ids[] = $this->auth->id;
			NoticeModel::where(['id' => $notice['id']])->update(['read_staff_ids' => implode(',', $read_staff_ids).',']);
		}

		$this->success('请求成功', $notice);
	}


}
